/**
 * 定义站点信息管理模块
 */
(function(window, angular, undefined){
	var Module = angular.module('water.apps.common.stationInfo.deviceAcceptManager', ['water.entity.monitorStation']);
	
	Module.controller('commonDeviceAcceptController',function($rootScope,$scope,$monitorStationService){

		/**
		 * 监听站点列表点击事件
		 */
		$scope.$on('dataPramChanged', function(event, data) {
			$scope.stationModel = data;
			$scope.getDeviceAcceptanceData(data.STATION_ID);
	    });
		
		//获取设备验收数据
		$scope.acceptData={};
		$scope.queryObj={};
		$scope.isShowAcceptDataSave=true;
		$scope.isShowReAccept=false;
		$scope.isShowAccept=false;
		$scope.queryObj.deviceAcceptQueryObj={"WSCLC_ID":"","stationCode":""};
			
		$scope.getDeviceAcceptanceData = function(stationId){
			var station_id = stationId;
			getStationList(station_id);
		};
			
		$scope.refreshAcceptData = function(stationCode){
			$("li[stationCode]").removeClass("selected");
			$("li[stationCode='"+stationCode+"']").addClass("selected");
			$scope.queryObj.deviceAcceptQueryObj.stationCode = stationCode;
			getAcceptData();
		}
			
		function getStationList(stationExpNo){
			if(!stationExpNo){
				return;
			}
			
			$scope.queryObj.deviceAcceptQueryObj.stationCode = stationExpNo;
			
			$waterDataCommon.getPageRows("/OnlineMonitor/getStationCodeList.do","stationListPS",$scope.queryObj.deviceAcceptQueryObj,10,1,function(result){
				if(result.data == null || result.data.length < 1){
					$scope.queryObj.deviceAcceptQueryObj.stationCode = "NONE";
				}else{
					$scope.queryObj.deviceAcceptQueryObj.stationCode=result.data[0].CODE;
					
					$("li[stationCode]").removeClass("selected");
					$("li[stationCode='"+$scope.queryObj.deviceAcceptQueryObj.stationCode+"']").addClass("selected");
				}
				getAcceptData();
			});
		}
			
		function getAcceptData(){
			$waterDataCommon.getPageRows("/OnlineMonitor/getDeviceAcceptanceData.do","deviceAcceptanceList",$scope.queryObj.deviceAcceptQueryObj,10,1,function(result){
				if(result.acceptData!=null && result.acceptData.length>0){
					$scope.isShowReAccept = true;
					$scope.isShowAccept = false;
				}
				else{
					
					$scope.isShowReAccept = false;
					$scope.isShowAccept = true;
				}
			});
		};
		//设备验收
		$scope.newAcceptInfo=function(){
			$scope.startChecked = false;
			var dtTmp = new Date();
			var dateStr = dtTmp.getFullYear()+"-"+(dtTmp.getMonth()+1)+"-"+dtTmp.getDate();
			$scope.acceptData={"ACCEP_DATE":dateStr,
					"STATUS":"1",
					"WSCLC_ID":$scope.queryObj.deviceAcceptQueryObj.WSCLC_ID,
					"STATION_CODE":$scope.queryObj.deviceAcceptQueryObj.stationCode};
			$scope.isShowAcceptDataSave = true;
			$itfreer.openWindow("acceptDevice");
		};
		//设备验收信息查看
		$scope.showAcceptInfo=function(obj,editable){
			$scope.acceptData = angular.copy(obj);
			$scope.isShowAcceptDataSave = editable;
			$itfreer.openWindow("acceptDevice");
		};
		//删除验收信息
		$scope.delAcceptInfo=function(obj){
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$itfreerHttp.deleteByKey("BC_DEVICE_ACCE_INFO", {GUID:obj.GUID},function(){
						$.messager.alert("提示","删除成功！","info");
						$scope.getDeviceAcceptanceData(obj.STATION_CODE);
					})
				}
			});
		};
		//查看验收记录
		$scope.showAcceptList = function(){
			$itfreer.openWindow("acceptDeviceList");
		};
		//保存设备验收信息
		$scope.saveAcceptInfo=function(isinvalid){
			$scope.startChecked = true;
			if(!isinvalid){
				return;
			}
			 $itfreerHttp.save('BC_DEVICE_ACCE_INFO',$scope.acceptData,function(data){
					$scope.getDeviceAcceptanceData($scope.acceptData.STATION_CODE);
					$itfreer.closeWindow('acceptDevice');
					$.messager.alert("提示","保存成功","info");
				}
			 );
		};
	});
}(window, angular));	